package com.sddz.pacs.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.sddz.pacs.entity.SysDict;
import com.sddz.pacs.mapper.SysDictMapper;
import com.sddz.pacs.service.SysDictService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.sddz.pacs.util.ShiroUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 系统字典信息表 服务实现类
 * </p>
 *
 * @author 时代电子
 * @since 2018-05-16
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {

    @Override
    public boolean updateDict(SysDict sysDict) {
        String userId = ShiroUtils.getUserId();
        sysDict.setUpdateDate(new Date());
        sysDict.setUpdateUserid(userId);
        boolean result = this.updateById(sysDict);
        return result;
    }

    @Override
    public boolean insertDict(SysDict sysDict) {
        String userId = ShiroUtils.getUserId();
        sysDict.setCreateDate(new Date());
        sysDict.setCreateUserid(userId);
        boolean result = this.insertOrUpdate(sysDict);
        return result;
    }

    @Override
    public boolean deleteDict(String dictId) {
        boolean result = this.deleteById(dictId);
        return result;
    }

    @Override
    public List<SysDict>  getDictList() {
        EntityWrapper<SysDict> ew = new EntityWrapper<>();
        List<SysDict> dictList = this.baseMapper.selectList(ew);

//        Map<String , Object> items = new HashMap<>();
//        // 获取每个节点的直属子节点，是直属，不是所有子节点
//        for(Integer i = 0; i< dictList.size(); i++){
//            String key = dictList.get(i).getParentId();
//            if(items.get(key)!=null){
//                ((List<SysDict>) items.get(key)).add(dictList.get(i));
//            }else{
//                List<SysDict> ls = new ArrayList<SysDict>();
//                ls.add(dictList.get(i));
//                items.put(key,ls);
//            }
//        }
//        List<SysDict> result = formatTree(items , "0");
//        return result;
        return dictList;
    }

    /**
     * 利用递归格式化每个节点
     */
    private List<SysDict> formatTree(Map items, String parentId){
        List<SysDict> result = new ArrayList<SysDict>();
        List<SysDict>  wTreeData = new ArrayList<SysDict>();
        if(items.get(parentId) == null){
            return result;
        }
        for (SysDict t : (List<SysDict>)items.get(parentId)){
            t.setChildren(formatTree(items,t.getId()));
            result.add(t);
        }
        return  result;
    }


    @Override
    public SysDict getDictById(String id) {
        SysDict sysDict = this.selectById(id);
        return sysDict;
    }

    //字典列表分层展示
    @Override
    public List<SysDict> getSysDictList(List<SysDict> list){

        List<SysDict> sysDicts = new ArrayList<>();
        for (SysDict sysDict : list){
            if (sysDict.getType().equals("0")){
                EntityWrapper<SysDict> ew1 = new EntityWrapper<>();
                ew1.eq("PARENT_ID",sysDict.getId());
                List<SysDict> sysDictList = this.baseMapper.selectList(ew1);
                if (sysDictList.size()>0){
                    sysDict.setChildren(getSysDictList(sysDictList));
                }
                sysDicts.add(sysDict);
            }

        }
        return sysDicts;
    }
}
